import { PureComponent } from "react"
import store from "../../../store"
import {
  changeAgeAction,
  changeNameAction
} from "../../../store/actionCreators"
import { connect } from "react-redux"
class Com2 extends PureComponent {
  // onSub(n) {
  //     store.dispatch(changeAgeAction(n));
  // }
  render() {
    const { age, changeAge } = this.props
    return (
      <div>
        <div>Com2:{age}</div>
        <div>
          <button
            onClick={() => {
              changeAge(-1)
            }}
          >
            -1
          </button>
          <button
            onClick={() => {
              changeAge(-2)
            }}
          >
            -2
          </button>
        </div>
      </div>
    )
  }
}

const mapStateToProps = (state) => ({
  age: state.age // 也就是说这里没写name，即使改变name也不会触发视图更新
})
const mapDispatchToProps = (dispatch) => ({
  changeAge: function (n) {
    dispatch(changeAgeAction(n))
  },
  changeName: function (name) {
    dispatch(changeNameAction(name))
  }
})
export default connect(mapStateToProps, mapDispatchToProps)(Com2)
